<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单编号">
                <a-input v-model="queryParam.id" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="充值者账户编号">
                <a-input v-model="queryParam.mobile" placeholder="" />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="被充值者编号">
                  <a-input v-model="queryParam.nickname" style="width: 100%" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
<!--        <a-button type="primary" icon="plus" @click="handleAdd()">新建</a-button>-->
        <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }} alert</a-button>
        <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1">
              <a-icon type="delete" />
              删除
            </a-menu-item>
            <!-- lock | unlock -->
            <a-menu-item key="2">
              <a-icon type="lock" />
              锁定
            </a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作
            <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ (pageNo - 1) * pageSize + index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:" @click="handleShowDetail(record.id)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:" @click="remove(record.id)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { Ellipsis, STable } from '@/components'
import { getRoleList } from '@/api/manage'
import orderApi from '@/api/order'
import StepByStepModal from '@/views/list/modules/StepByStepModal'
import rechargeOrderForm from '@/views/list/modules/RechargeOrderForm'
import Info from '@/views/list/components/Info'

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    StepByStepModal,
    rechargeOrderForm,
    Info
  },
  data () {
    return {
      visible: false,
      confirmLoading: false,
      mdl: {},
      total: 0, // 数据库中的总记录数
      pageNo: 1, // 默认页码
      pageSize: 10, // 每页记录数
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '#',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '订单编号',
          dataIndex: 'id'
        },
        {
          title: '充值者用户名',
          dataIndex: 'param.rechargerUsername'
        },
        {
          title: '被充值者用户名',
          dataIndex: 'param.rechargedUsername'
        },
        {
          title: '充值金额',
          dataIndex: 'rechargeAmount'
        },
        {
          title: '订单状态',
          dataIndex: 'param.stringOrderStatus',
          needTotal: true
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          sorter: true
        },
        {
          title: '更新时间',
          dataIndex: 'updateTime',
          sorter: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // console.log('loadData.parameter', parameter)
        return orderApi.getRechargeOrderPageList(parameter, this.queryParam)
          .then(res => {
            console.log(res.data)
            this.total = res.data.total
            this.pageNo = res.data.current
            this.pageSize = res.data.size
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    this.tableOption()
    getRoleList({ t: new Date() })
  },
  methods: {
    handleAdd () {
      const _this = this
      this.$dialog(rechargeOrderForm,
        // component props
        {
          record: {},
          on: {
            ok (e) {
              console.log('ok 回调')
              // console.log(e)
              orderApi.add(e).then(res => {
                if (res.code === 201) {
                  console.log(res.message)
                  _this.$message.error(res.message)
                  // this.visible = false
                } else {
                  _this.$refs.table.refresh(true)
                  // this.visible = false
                  _this.$message.success('添加订单信息成功!')
                }
              })
              // console.log(Promise)
            },
            cancel () {
              console.log('cancel 回调')
            },
            close () {
              console.log('modal close 回调')
            }
          }
        },
        // modal props
        {
          title: '新增',
          width: 700,
          centered: true,
          maskClosable: false
        })
    },
    handleEdit (record) {
      console.log('record', record)
      const _this = this
      this.$dialog(rechargeOrderForm,
        // component props
        {
          record,
          on: {
            ok (e) {
              console.log('ok 回调')
              // console.log(e)
              orderApi.editRechargeOrder(e).then(res => {
                if (res.code === 201) {
                  console.log(res.message)
                  _this.$message.error(res.message)
                } else {
                  _this.$refs.table.refresh(true)
                  // this.visible = false
                  _this.$message.success('修改充值订单信息成功!')
                }
              })
              // console.log(MemInfoForm)
            },
            cancel () {
              console.log('cancel 回调')
            },
            close () {
              console.log('modal close 回调')
            },
            submit () {
              console.log('submit 回调')
            }
          }
        },
        // modal props
        {
          title: '编辑',
          width: 700,
          centered: true,
          maskClosable: false
        })
    },
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: {
            show: true,
            clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },

    lock (id, isDisabled) {
      orderApi.lock(id, isDisabled).then(res => {
        if (res.code === 200 && isDisabled === false) {
          this.$refs.table.refresh(true)
          this.$message.info('已启用')
        } else if (res.code === 200 && isDisabled === true) {
          this.$refs.table.refresh(true)
          this.$message.info('已禁用')
        }
      })
    },

    remove (id) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '此操作将永久删除该订单信息,是否继续?',
        okText: 'Yes',
        okType: 'danger',
        cancelText: 'No',
        onOk () {
          orderApi.remove(id).then(res => {
            if (res.code === 200) {
              _this.$refs.table.refresh(true)
              _this.$message.info('删除成功')
            } else {
              _this.$refs.table.refresh(true)
              _this.$message.info('删除失败')
            }
          })
        }
      })
    },

    handleShowDetail (id) {
      this.$router.push({ name: 'rechargeDetail', params: { id: id } })
    },

    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },

    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
